import java.util.ArrayList;
import java.util.List;

public class Solution1 {
    public static void main(String[] args) {
        System.out.println(generateParenthesis(3));
    }
    public static int left = 0;
    public static int right = 0;
    public static int n = 0;
    public static StringBuffer path = new StringBuffer();
    public static List<String> ret = new ArrayList<>();
    public static List<String> generateParenthesis(int _n) {
        n = _n;
        dfs();
        return ret;
    }

    public static void dfs() {
        if(right == n) {
            ret.add(path.toString());
            return;
        }

        if(left < n) { // 添加左括号
            path.append('(');
            left++;
            dfs();
            path.deleteCharAt(path.length() - 1); // 恢复现场
            left--;
        }

        if(right < left) { // 添加右括号
            path.append(')');
            right++;
            dfs();
            path.deleteCharAt(path.length() - 1); // 恢复现场
            right--;
        }
    }
}
